package com.lvan.consumer.receive;

import com.lvan.consumer.channel.MyProcessor;
import com.lvan.consumer.send.SmsAndEmailMessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

/**
 * @author luom
 * @date 2021/9/3
 */
@Component
@EnableBinding(MyProcessor.class)
public class SourceMessageConsumer {

    private final Logger logger = LoggerFactory.getLogger(SourceMessageConsumer.class);

    @Autowired
    private SmsAndEmailMessageProducer smsAndEmailMessageProducer;

    @StreamListener(MyProcessor.SOURCE_MESSAGE)
    private void receive(String sourceMessage) {
        logger.info("原始消息接收成功，原始消息为:{}", sourceMessage);
        // 发送消息， 号码
        smsAndEmailMessageProducer.sendSms(sourceMessage.split("\\|")[0]);
        // 发送消息， 邮件
        smsAndEmailMessageProducer.sendEmail(sourceMessage.split("\\|")[1]);
    }

}
